<template>
  <div class="form-container">
    <el-form inline :model="tagForm" ref="refTagForm" class="form-line" label-width="80">
      <el-form-item label="标签名称" prop="labelName">
        <el-input size="default" v-model="tagForm.labelName" placeholder="请输入标签名称"></el-input>
      </el-form-item>
      <el-form-item label="适用范围" prop="labelScope">
        <el-select size="default" v-model="tagForm.labelScope" placeholder="请选择范围">
          <el-option label="集品到家" value="1"></el-option>
          <el-option label="集品同城" value="2"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="适用等级" prop="labelLevel">
        <el-select size="default" v-model="tagForm.labelLevel" placeholder="请选择等级">
          <el-option label="非常差" value="1"></el-option>
          <el-option label="差" value="2"></el-option>
          <el-option label="一般" value="3"></el-option>
          <el-option label="满意" value="4"></el-option>
          <el-option label="非常满意" value="5"></el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <div class="btn-container">
      <el-button type="primary" @click="handleReset(refTagForm as FormInstance)">重置</el-button>
      <el-button type="primary" @click="getTagList">查询</el-button>
      <el-button type="primary" @click="handleAdd(refTagForm as FormInstance)">添加</el-button>
    </div>
  </div>
  <div class="table-container">
    <el-table
      border
      :cell-style="{ textAlign: 'center', fontSize: '13px' }"
      :header-cell-style="{
        textAlign: 'center',
        fontWeight: '100',
        fontSize: '13px',
        color: 'white',
        backgroundColor: '#009688'
      }"
      :data="tagList"
      style="width: 100%"
    >
      <el-table-column fixed prop="labelName" label="标签" />
      <el-table-column prop="labelScope" label="适用范围">
        <template #default="scope">{{ scope.row.labelScope === "1" ? "集品到家" : "集品同城" }}</template>
      </el-table-column>
      <el-table-column prop="labelLevel" label="适用等级">
        <template #default="scope">{{
          scope.row.labelLevel === "1"
            ? "非常差"
            : scope.row.labelLevel === "2"
              ? "差"
              : scope.row.labelLevel === "3"
                ? "一般"
                : scope.row.labelLevel === "4"
                  ? "满意"
                  : "非常满意"
        }}</template>
      </el-table-column>
      <el-table-column prop="createTime" label="创建时间" />
      <el-table-column fixed="right" label="操作">
        <template #default="scope">
          <el-button link type="primary" size="small" @click="handleDelete(scope.row.id)">删除</el-button>
          <el-button link type="primary" size="small" @click="handleEdit(scope.row)"> 修改 </el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="pagination-container">
      <el-pagination
        background
        size="small"
        v-model:current-page="tagForm.pageNum"
        :page-sizes="[5, 10, 15, 20]"
        layout="prev, pager, next"
        @change="getTagList"
        :total="pageTotal"
        :page-size="tagForm.pageSize"
      />
    </div>
  </div>
  <el-dialog v-model="dialogFormVisible" :title="titleMsg" width="500">
    <el-form :model="addTagForm" ref="refAddTagForm" :rules="addTagFormRules" class="form-line" label-width="100px">
      <el-form-item label="适用范围" prop="labelScope">
        <el-select size="default" v-model="addTagForm.labelScope" placeholder="请选择范围">
          <el-option label="集品到家" value="1"></el-option>
          <el-option label="集品同城" value="2"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="适用等级" prop="labelLevel">
        <el-select size="default" v-model="addTagForm.labelLevel" placeholder="请选择等级">
          <el-option label="非常差" value="1"></el-option>
          <el-option label="差" value="2"></el-option>
          <el-option label="一般" value="3"></el-option>
          <el-option label="满意" value="4"></el-option>
          <el-option label="非常满意" value="5"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="标签名称" prop="labelName">
        <el-input size="default" v-model="addTagForm.labelName" placeholder="请输入标签名称"></el-input>
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取消</el-button>
        <el-button type="primary" @click="submitTagForm(refAddTagForm as FormInstance)">{{
          titleMsg === "添加标签" ? "添加" : "修改"
        }}</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup lang="tsx" name="useProTable">
import { onMounted, ref } from "vue";
import { Tag } from "@/api/interface/province/config";
import { ElMessage, ElMessageBox, FormInstance, FormRules } from "element-plus";
import { addTagAPI, deleteTagAPI, getTagListAPI, updateTagAPI } from "@/api/modules/province/configAPI";
let dialogFormVisible = ref<boolean>(false);
let pageTotal = ref<number>(0);
let titleMsg = ref<string>("添加标签");
let tagForm = ref<Tag.SearchParams>({
  labelName: "",
  labelScope: "",
  labelLevel: "",
  pageNum: 1,
  pageSize: 10
});
let addTagForm = ref<Tag.UpdateParams>({
  labelName: "",
  labelScope: "",
  labelLevel: ""
});
const refAddTagForm = ref<FormInstance>();
const addTagFormRules = ref<FormRules>({
  labelName: [{ required: true, message: "请输入标签名称", trigger: "blur" }],
  labelScope: [{ required: true, message: "请选择适用范围", trigger: "blur" }],
  labelLevel: [{ required: true, message: "请选择适用等级", trigger: "blur" }]
});
const tagList = ref<Tag.Row[]>([
  {
    id: 1,
    labelName: "string",
    labelScope: "st",
    labelLevel: "st",
    createTime: "2019-08-24T14:15:22.123Z"
  },
  {
    id: 2,
    labelName: "string",
    labelScope: "st",
    labelLevel: "st",
    createTime: "2019-08-24T14:15:22.123Z"
  },
  {
    id: 3,
    labelName: "string",
    labelScope: "st",
    labelLevel: "st",
    createTime: "2019-08-24T14:15:22.123Z"
  },
  {
    id: 4,
    labelName: "string",
    labelScope: "st",
    labelLevel: "st",
    createTime: "2019-08-24T14:15:22.123Z"
  }
]);
const refTagForm = ref<FormInstance>();
const handleReset = (refTagForm: FormInstance) => {
  refTagForm.resetFields();
  getTagList();
};
const handleDelete = (id: number) => {
  ElMessageBox.confirm("确认删除吗?", "提示", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning"
  })
    .then(() => {
      deleteTag(id);
    })
    .catch(() => {
      ElMessage.info("已取消删除");
    });
};
const handleAdd = (formEl: FormInstance) => {
  titleMsg.value = "添加标签";
  formEl.resetFields();
  addTagForm.value = {
    labelName: "",
    labelScope: "",
    labelLevel: ""
  };
  dialogFormVisible.value = true;
};
const handleEdit = (row: Tag.Row) => {
  titleMsg.value = "修改标签";
  console.log(row.createTime);
  addTagForm.value.id = row.id;
  addTagForm.value.labelName = row.labelName as string;
  addTagForm.value.labelScope = row.labelScope;
  addTagForm.value.labelLevel = row.labelLevel;
  dialogFormVisible.value = true;
};
const submitTagForm = (formEl: FormInstance) => {
  if (addTagForm.value) {
    formEl.validate((valid: boolean) => {
      if (valid) {
        titleMsg.value === "添加标签" ? addTag() : updateTag();
      } else {
        ElMessage.error("请填写完整信息");
      }
    });
  }
};
const getTagList = async () => {
  const { rows, code, msg, total } = await getTagListAPI(tagForm.value);
  if (code == "200" && rows && total) {
    tagList.value = rows;
    pageTotal.value = total;
  } else {
    ElMessage.error(msg);
  }
};
const addTag = async () => {
  const { code, msg } = await addTagAPI(addTagForm.value);
  if (code == "200") {
    ElMessage.success("添加成功");
    dialogFormVisible.value = false;
    getTagList();
  } else {
    ElMessage.error(msg);
  }
};
const updateTag = async () => {
  const { code, msg } = await updateTagAPI(addTagForm.value);
  if (code == "200") {
    ElMessage.success("修改成功");
    dialogFormVisible.value = false;
    getTagList();
  } else {
    ElMessage.error(msg);
  }
};
const deleteTag = async (id: number) => {
  const { code, msg } = await deleteTagAPI(id);
  if (code == "200") {
    ElMessage.success("删除成功");
    getTagList();
  } else {
    ElMessage.error(msg);
  }
};
onMounted(() => {
  getTagList();
});
</script>
<style lang="scss" scoped>
::v-deep .el-button > span {
  font-size: 13px;
}
::v-deep .el-form-item__label {
  font-size: 13px;
}
::v-deep .el-input__inner {
  font-size: 13px;
}
::v-deep .el-select_wapper {
  font-size: 13px;
}
::v-deep .el-select__placeholder.is-transpar {
  font-size: 13px;
}
.form-container {
  padding: 10px;
  margin-bottom: 10px;
  background-color: white;
  .btn-container {
    display: inline-block;
    float: right;
    vertical-align: middle;
  }
  .el-form {
    display: inline-block;
  }
  .form-line {
    .el-form-item {
      width: 280px;
      margin: 0;
      margin-right: 10px;
    }
    .el-input {
      width: 240px;
    }
    .el-select {
      width: 240px;
    }
  }
}
.table-container {
  padding: 40px 10px;
  padding-bottom: 60px;
  background-color: white;
  .pagination-container {
    // display: inline-block;
    float: right;
    margin-top: 20px;
    background-color: white;
  }
}
</style>
